package com.atguigu.hive.util;

import com.atguigu.constant.CommonConstantValue;
import org.apache.log4j.Logger;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * @ClassName HiveUtil
 * @DeScription Hive工具类
 * @Author 剧情再美终是戏
 * @Date 2019/11/25 18:16
 * @Version 1.0
 **/
public class HiveUtil {

    private static Logger logger = Logger.getLogger(HiveUtil.class);

    private HiveUtil() {

    }

    /**
     * 获取hive连接
     *
     * @param url      连接的url
     * @param username 用户
     * @param password 密码
     * @return java.sql.Connection
     * @Author 剧情再美终是戏
     * @Date 2019/11/25
     * @Version 1.0
     **/
    public static Connection getConnection(String url, String username, String password) throws SQLException {
        Connection connection = DriverManager.getConnection(url, username, password);
        if (null == connection) {
            logger.error("获取hive连接失败");
        }
        logger.debug("connection success : " + connection);
        return connection;
    }

    /**
     * 关闭连接
     *
     * @param connection 连接实例
     * @return void
     * @Author 剧情再美终是戏
     * @Date 2019/11/25
     * @Version 1.0
     **/
    public static void closeConnection(Connection connection) throws SQLException {
        if (null == connection) {
            return;
        }
        connection.close();
    }
}
